using GameToolkit.BehaviourTree;

namespace GameToolkit
{
    [System.Serializable]
    [CompositeCategory]
    [CompositeTitle("循环执行", Description = "Loop Controller")]
    [CompositeIcon("8b269d257ae3cbf499f5b3497032265d")]
    public class LoopController : ExecutableComposite
    {
        public override BehaviourTreeObject Create()
        {
            return new Impl();
        }

        class Impl : ControllerNode, ILoopController
        {
            int mIndex;

            protected override NodeState GetNextBehaviour(BehaviourTreePlayer player, out ExecutableEntry nextNode)
            {
                if (mIndex < ChildCount)
                    nextNode = GetChild(mIndex++);
                else
                    nextNode = null;
                return NodeState.Running;
            }

            protected override void OnCreate(BehaviourTreePlayer player)
            {
            }

            protected override void OnDestroy(BehaviourTreePlayer player)
            {
            }

            protected override void OnStart(BehaviourTreePlayer player)
            {
                mIndex = 0;
            }

            protected override void OnStop(BehaviourTreePlayer player)
            {
            }

            void ILoopController.OnResetLoop(BehaviourTreePlayer player)
            {
                mIndex = 0;
            }
        }
    }
}
